<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">围群博客后台管理系统</h3>
      <el-form-item prop="accountPhone">
        <el-input v-model="loginForm.accountPhone" type="text" auto-complete="off" placeholder="账号"
          prefix-icon="el-icon-user-solid">
          <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
        </el-input>
      </el-form-item>
      <el-form-item prop="accountPass">
        <el-input v-model="loginForm.accountPass" placeholder="密码" show-password prefix-icon="el-icon-lock"
          @keyup.enter.native="handleLogin">
          <!-- <svg-icon slot="prefix" icon-class="accountPass" class="el-input__icon input-icon" /> -->
        </el-input>
      </el-form-item>
      <!-- <el-form-item prop="imgCode">
        <el-input class="imgCode" v-model="loginForm.imgCode" placeholder="图形验证码"></el-input>
        <s-identify :identifyCode="identifyCode"></s-identify>
      </el-form-item> -->
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
          @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <Vcode :show="isShow" @success="success" @close="close"/>
  </div>
</template>
    
<script>
import Vcode from "vue-puzzle-vcode";
export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      isShow: false, // 验证码模态框是否出现
      loginForm: {
        accountPhone: "",
        accountPass: "",
        imgCode:"",
        rememberMe:false,//是否保存密码
      },
      loginRules: {
        accountPhone: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        accountPass: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 注册开关
      register: true,
      redirect: undefined,
      
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getPhoneAndPass();
  },
  components: {
    Vcode
  },
  methods: {

    getPhoneAndPass(){
      if(JSON.parse(localStorage.getItem("loginForm"))){
        this.loginForm = JSON.parse(localStorage.getItem("loginForm"));
      }
      
    },

    changeCheckCode(_img){//验证码点击刷新
        _img.src = "checkCode?" + new Date().getTime();
    },

    // 用户通过了验证
    success(msg) {
      this.isShow = false; // 通过验证后，需要手动隐藏模态框
      this.$axios.post("api/testLoginController/login", this.loginForm).then((res) => {
        console.log(res);
        if (res.data.success) {
          this.$message({
            message: '登录成功',
            type: 'success'
          })
          if(this.loginForm.rememberMe){//记住账号密码
            localStorage.setItem("loginForm",JSON.stringify(this.loginForm));
          }else if(JSON.parse(localStorage.getItem("loginForm"))){
            localStorage.removeItem("localStorage");
          }
          sessionStorage.setItem("userMess",JSON.stringify(res.data.data.login));
          this.$router.push({
            name: 'index'
          })
        }else{
          this.$message({
            message: res.data.message,
            type: 'error'
          })
        }
        this.loading = false;
      });
    },
    // 用户点击遮罩层，应该关闭模态框
    close() {
      this.isShow = false;
      this.loading = false;
    },

    handleLogin() {
      this.loading = true;
      this.isShow = true;
    }
  }
};
</script>
  
<style scoped>

.imgCode{
  width: 50%;
}

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 700px;
  background-image: url("../../assets/images/login-background.jpg");
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
  
  
    
    